
import java.awt.Desktop;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Properties;
import javax.swing.JOptionPane;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author fabian
 */
public class makeconf {
    private Twitter twitter;
    public  makeconf(){
        try {
                    twitter = new TwitterFactory().getInstance();
                    int trytime = 0;
                    try {
                        twitter.setOAuthConsumer("TuSvVl1GoSbSWoL0Hm0Pg", "rtNqaOH3rP96PeCvhkfVQR49KaxONDOfeyCRyKmbQRM");
                        RequestToken requestToken = twitter.getOAuthRequestToken();
                        AccessToken accessToken = null;
                        while (null == accessToken) {
                            if(trytime!=0){
                            int optt = JOptionPane.showConfirmDialog(null, "Pin inválido. Presione OK si desea reintentar.", "ERROR!", JOptionPane.ERROR_MESSAGE);
                            if(optt != JOptionPane.OK_OPTION){
                               System.exit(0);
                           }
                           }
                           int opt = JOptionPane.showConfirmDialog(null, "haga click en ok para redirigirlo a un navegador,\nluego ingrese el pin suministrado en el sitio","Primer uso!", JOptionPane.OK_CANCEL_OPTION);
                           if(opt != JOptionPane.OK_OPTION){
                               System.exit(0);
                           }
                           try {
                                Desktop.getDesktop().browse(new URI(requestToken.getAuthorizationURL()));
                            } catch (IOException ignore) {
                            } catch (URISyntaxException e) {
                                throw new AssertionError(e);
                            }
                           try {
                               Thread.currentThread().sleep(2000);
                           } catch (InterruptedException e){
                           }
                           String pin = JOptionPane.showInputDialog(null, "PIN", "Ingrese el Pin suministrado en la pagina", JOptionPane.QUESTION_MESSAGE);
                            try {
                                if (pin.length() > 0) {
                                    accessToken = twitter.getOAuthAccessToken(requestToken, pin);
                                } else {
                                    accessToken = twitter.getOAuthAccessToken(requestToken);
                                }
                            } catch (TwitterException te) {
                                if (401 == te.getStatusCode()) {
                                } else {
                                }
                            }
                           trytime++;
                        }
                        File file = new File("twitter4j.properties");
                        Properties prop = new Properties();
                        OutputStream os = new FileOutputStream(file);
                        prop.setProperty("oauth.consumerKey", "TuSvVl1GoSbSWoL0Hm0Pg");
                        prop.setProperty("oauth.consumerSecret","rtNqaOH3rP96PeCvhkfVQR49KaxONDOfeyCRyKmbQRM");
                        prop.setProperty("oauth.accessToken", accessToken.getToken());
                        prop.setProperty("oauth.accessTokenSecret", accessToken.getTokenSecret());
                        prop.store(os, "twitter4j.properties");
                        os.close();
                        JOptionPane.showMessageDialog(null, "Felicidades! esta listo para utilizar twitelo","Configuracion Finalizada",JOptionPane.INFORMATION_MESSAGE);

                    } catch (IllegalStateException ie) {
                        if (!twitter.getAuthorization().isEnabled()) {
                            System.exit(-1);
                        }
                    }
                } catch (TwitterException te) {
                    System.exit(-1);
                } catch (IOException ioe) {
                    System.exit(-1);
                }
    }
    public Twitter get_tw(){
        return twitter;
    }
}
